package string;

public class 不使用库函数的字符串转整数 {
    class Solution {
        public int myAtoi(String str) {
            System.out.println();
            char[] c = str.trim().toCharArray();
            if(c.length == 0)
                return 0;
            int res = 0,bndry = Integer.MAX_VALUE/10;
            int i=1,sign=1;
            if(c[0]=='-') sign = -1;
            else if(c[0] !='+') i = 0;
            for(int j=i;j<c.length;j++){
                if(c[j]<'0'||c[j]>'9') break;
                if(res > bndry || res == bndry && c[j]>'7')
                    return sign == 1 ? Integer.MAX_VALUE: Integer.MIN_VALUE;
                //2147483647
                //-2147483648
                res = res*10 +(c[j] - '0');
            }
            return res*sign;
        }
    }
}
